import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { render } from '/@/utils/common/renderUtils';
import { defHttp } from '/@/utils/http/axios';
// import { rules } from '/@/utils/helper/validator';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '名称',
    align: 'center',
    dataIndex: 'name',
    width: 300,
  },
  {
    title: '医院',
    align: 'center',
    dataIndex: 'hospitalId',
    customRender: ({ text, record }) => {
      // 优先使用后端返回的字典文本字段
      if (record?.hospitalId_dictText) {
        return record.hospitalId_dictText;
      }
      // 兼容 hospital_id_dictText
      if (record?.hospital_id_dictText) {
        return record.hospital_id_dictText;
      }
      // 如果没有字典文本字段，使用字典渲染
      const hospitalId = text || record?.hospitalId || record?.hospital_id;
      if (hospitalId) {
        const dictText = render.renderDict(hospitalId, 'nurse_hospital,name,id');
        if (dictText) {
          return dictText;
        }
        // 如果字典渲染失败，直接显示ID
        return hospitalId;
      }
      return '-';
    },
  },
  {
    title: '价格',
    align: 'center',
    dataIndex: 'price',
  },
  {
    title: '类型',
    align: 'center',
    dataIndex: 'type',
  },
  {
    title: '编号',
    align: 'center',
    dataIndex: 'code',
    width: 150,
  },
  {
    title: '是否默认',
    align: 'center',
    dataIndex: 'isDefault_dictText',
    width: 150,
    customRender: ({ text, record }) => {
      // 如果 _dictText 字段有值，直接显示
      if (text) {
        return text;
      }
      // 如果没有，则使用原始值进行字典转换
      return render.renderDict(record.isDefault, 'yes_no');
    },
  },
];

//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '医院',
    field: 'hospitalId',
    component: 'JSearchSelect',
    componentProps: {
      dict: 'nurse_hospital,name,id',
      placeholder: '请选择医院',
    },
    //colProps: {span: 6},
  },
  {
    label: '名称',
    field: 'name',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: '编号',
    field: 'code',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: '类型',
    field: 'type',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: '是否默认',
    field: 'isDefault',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'yes_no',
    },
    //colProps: {span: 6},
  },
];

//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '名称',
    field: 'name',
    component: 'Input',
  },
  {
    label: '价格',
    field: 'price',
    component: 'InputNumber',
  },
  {
    label: '类型',
    field: 'type',
    component: 'Input',
  },
  {
    label: '编号',
    field: 'code',
    component: 'Input',
  },
  {
    label: '是否默认',
    field: 'isDefault',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'yes_no',
    },
  },
  {
    label: '医院',
    field: 'hospital_id',
    component: 'ApiSelect',
    componentProps: ({ formActionType, formModel }) => {
      return {
        api: async () => {
          // 获取全部医院，不添加任何过滤参数
          const result = await defHttp.get({
            url: '/nurse_hospital/nurseHospital/rootList',
            params: {
              pageNo: 1,
              pageSize: 99999,
            },
          });
          if (result && result.records && Array.isArray(result.records)) {
            return result.records.map((item) => ({
              label: item.name,
              value: item.id,
            }));
          }
          return [];
        },
        labelField: 'label',
        valueField: 'value',
        placeholder: '请选择或搜索医院',
        showSearch: true,
        allowClear: true,
        getPopupContainer: () => document.body,
        dropdownStyle: { zIndex: 9999 },
      };
    },
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

// 高级查询数据
export const superQuerySchema = {
  sysOrgCode: { title: '所属部门', order: 0, view: 'text', type: 'string' },
  name: { title: '名称', order: 1, view: 'text', type: 'string' },
  price: { title: '价格', order: 2, view: 'number', type: 'number' },
  type: { title: '类型', order: 3, view: 'text', type: 'string' },
  code: { title: '编号', order: 4, view: 'text', type: 'string' },
  isDefault: { title: '是否默认', order: 5, view: 'radio', type: 'string', dictCode: 'yes_no' },
  hospitalId: { title: '医院', order: 6, view: 'list', type: 'string', dictTable: "nurse_hospital", dictCode: 'id', dictText: 'name' },
};
